package com.java110.mall.user.cmd.register;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.core.factory.AliSendMessageFactory;
import com.java110.core.factory.SendSmsFactory;
import com.java110.core.factory.TencentSendMessageFactory;
import com.java110.dto.user.UserDto;
import com.java110.intf.user.IUserV1InnerServiceSMO;
import com.java110.utils.cache.CommonCache;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.List;

/**
 * 获取平台注册时的手机验证码
 */
@Java110Cmd(serviceCode = "register.messageCode")
public class PcRegisterCodeCmd extends Cmd {

    @Autowired
    private IUserV1InnerServiceSMO userV1InnerServiceSMO;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        Assert.hasKeyAndValue(reqJson, "tel", "手机号不能为空");
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        UserDto userDto = new UserDto();
        userDto.setTel(reqJson.getString("tel"));
        List<UserDto> userDtos = userV1InnerServiceSMO.queryUsers(userDto);
        if (userDtos.size() > 0){
            context.setResponseEntity(new ResponseEntity<>("该号码已被注册，请更换手机号。", HttpStatus.OK));
            return;
        }
        String oldCode = CommonCache.getValue(reqJson.getString("tel") + "_validateTel_resend");
        ResponseEntity<String> sendMessageResult = null;
        String verifyStr = "";
        if(!StringUtil.isNullOrNone(oldCode)){
            verifyStr = "请稍后重试";
            sendMessageResult = new ResponseEntity<>(verifyStr, HttpStatus.OK);
            context.setResponseEntity(sendMessageResult);
            return;
        }
        String verifyCode = AliSendMessageFactory.generateMessageCode();
        verifyStr = "演示环境验证码:" + verifyCode;
        try {
            if ("ON".equals(MappingCache.getValue(MappingConstant.SMS_DOMAIN, SendSmsFactory.SMS_SEND_SWITCH))) {
                //开始发送验证码
                //AliSendMessageFactory.sendMessage(reqJson.getString("tel"), verifyCode);
                TencentSendMessageFactory.sendMessage(reqJson.getString("tel"), verifyCode);
                //SendSmsFactory.sendSms(reqJson.getString("tel"), verifyCode);
                verifyStr = "验证码已下发至您的手机!";
            }
            //将验证码存入Redis中
            CommonCache.setValue(reqJson.getString("tel") + "_validateTel", verifyCode.toLowerCase(), CommonCache.defaultExpireTime);
            //将验证码存入Redis中
            CommonCache.setValue(reqJson.getString("tel") + "_validateTel_resend", verifyCode.toLowerCase(), CommonCache.RESEND_DEFAULT_EXPIRETIME);
            sendMessageResult = new ResponseEntity<>(verifyStr, HttpStatus.OK);
            context.setResponseEntity(sendMessageResult);
            return;
        } catch (Exception e) {
            sendMessageResult = new ResponseEntity<>("验证码服务系统异常", HttpStatus.INTERNAL_SERVER_ERROR);
            context.setResponseEntity(sendMessageResult);
            return;
        }

    }
}
